package com.reef.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.reef.dto.BookClassDto;
import com.reef.entity.Book_class;
import com.reef.entity.Result;
import com.reef.service.BookcategoryService;
@RestController
public class BookcategoryController {
	@Autowired
     private BookcategoryService bookcategoryService;
	/**
	 * 多条件动态查询
	 */
	@RequestMapping("/querybookcateAll")
	public Object findBybookclassCondition(BookClassDto bcDto,int page,int limit) {
		//getTotalElements()指实体中有多少元素
		//分页查询 springBoot2.0以下版本用new 实例化，2.0以上版本使用的是PageRequest
		Pageable pageable = PageRequest.of(page-1, limit);
		Page<Book_class> pagelist  = bookcategoryService.findByBookclassCondition(bcDto, pageable);
		Map<String, Object> map =new HashMap<String, Object>();
		map.put("code", 0);
		map.put("msg", "");
		map.put("count", pagelist.getTotalElements());
		map.put("data", pagelist.getContent());
		return map;
	}
	//添加图书类别
	@RequestMapping("/insertbookcategory")
	public Object addbook(Book_class book_class) {
		Book_class i=bookcategoryService.insertBookclass(book_class);
		if(i!=null) {
			Result result = new Result("添加成功","",true);
			return result;
		}else {
			Result result = new Result("添加失败","",false);
			return result;
		}
		
	}
	//修改图书类别
		@RequestMapping("/updatebookcategory")
		public Object updateBookclass(Book_class book_class) {
			Book_class i=bookcategoryService.insertBookclass(book_class);
			if(i!=null) {
				Result result = new Result("修改成功","",true);
				return result;
			}else {
				Result result = new Result("修改失败","",false);
				return result;
			}
			
		}

}
